﻿namespace OsPortal
{
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Web.UI.WebControls;
    using System.Xml;

    public class oDataHelper
    {
        public static DateTime DateTimeMinValue = DateTime.MinValue;
        public static int IntMinValue = -2147483648;

        protected void AddSqlParameter(DbCommand dbCommand, SqlParameter[] parameters)
        {
            if (parameters != null)
            {
                dbCommand.Parameters.Clear();
                foreach (SqlParameter parameter in parameters)
                {
                    if ((parameter.Value != null) && (parameter.Value != DBNull.Value))
                    {
                        switch (parameter.DbType)
                        {
                            case DbType.AnsiString:
                            case DbType.String:
                            {
                                dbCommand.Parameters.Add(new SqlParameter(parameter.ParameterName, EscapeQuoteReg(parameter.Value.ToString())));
                                continue;
                            }
                        }
                        dbCommand.Parameters.Add(new SqlParameter(parameter.ParameterName, parameter.Value));
                    }
                }
            }
        }

        protected void AddSqlParameter(ParameterCollection ParamCol, SqlParameter[] parameters)
        {
            if (parameters != null)
            {
                ParamCol.Clear();
                foreach (SqlParameter parameter in parameters)
                {
                    if ((parameter.Value != null) && (parameter.Value != DBNull.Value))
                    {
                        switch (parameter.DbType)
                        {
                            case DbType.AnsiString:
                            case DbType.String:
                            {
                                ParamCol.Add(parameter.ParameterName, EscapeQuoteReg(parameter.Value.ToString()));
                                continue;
                            }
                        }
                        ParamCol.Add(parameter.ParameterName, parameter.Value.ToString());
                    }
                }
            }
        }

        public static string AppSettingsWebConfig(string strKey)
        {
            string str = ConfigurationManager.AppSettings[strKey];
            if ((str == null) || (str.Length == 0))
            {
                return string.Empty;
            }
            return str.Trim();
        }

        public static string ConnectionStringWebConfig(string strKey)
        {
            string str = "Data Source=112.213.94.110;Initial Catalog=AutoSendMail;User ID=admin;PWD=phuhaigia";
            if ((str == null) || (str.Length == 0))
            {
                return string.Empty;
            }
            return str.Trim();
        }

        public static string EscapeName(string S)
        {
            if (S.IndexOfAny(new char[] { '[', ']', '*', '.', ' ' }) != -1)
            {
                return S;
            }
            return string.Format("[{0}]", S);
        }

        public static string EscapeQuoteReg(string S)
        {
            return Regex.Replace(S.Replace('\'', '"'), @"(?:delete|select|drop|create|xp_)\s", string.Empty, RegexOptions.IgnoreCase);
        }

        public static DataTable filterData(DataTable dt, string strwhere)
        {
            DataRow[] rowArray = dt.Select(strwhere);
            DataTable table = dt.Clone();
            foreach (DataRow row in rowArray)
            {
                table.ImportRow(row);
            }
            return table;
        }

        public static DataSet GetDataSetFromRss(string Path)
        {
            DataSet set;
            try
            {
                using (DataSet set2 = new DataSet())
                {
                    using (Stream stream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        using (XmlTextReader reader = new XmlTextReader(stream))
                        {
                            set2.ReadXml(reader);
                            return set2;
                        }
                    }
                }
            }
            catch
            {
                set = null;
            }
            return set;
        }

        public static DataTable GetDataTableFromRssCache(string Path)
        {
            DataTable table = GetDataSetFromRss(Path).Tables[0];
            if (table.Rows.Count > 0)
            {
                return table;
            }
            return null;
        }

        public static string InsertQuote(string S)
        {
            return string.Format("'{0}'", EscapeQuoteReg(S));
        }

        public static bool isnull(object obValue)
        {
            return ((obValue == null) || (obValue == DBNull.Value));
        }

        public static int IsNull(object obValue, int intDefault)
        {
            return (isnull(obValue) ? intDefault : ((int) obValue));
        }

        public static object IsNullIfEqual0(int Param)
        {
            if (Param == 0)
            {
                return null;
            }
            return Param;
        }

        protected bool IsStoredProcedure(string strQuerry)
        {
            return !Regex.IsMatch(strQuerry.Trim(), @"(?:\;|\s)?(select|insert|update|delete|drop)\s", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        }

        protected void SetCommand(SqlCommand command, string strQuerry, SqlParameter[] parameters)
        {
            command.CommandTimeout = 300;
            if (this.IsStoredProcedure(strQuerry))
            {
                command.CommandType = CommandType.StoredProcedure;
            }
            command.Parameters.Clear();
            if (parameters != null)
            {
                foreach (SqlParameter parameter in parameters)
                {
                    if (parameter.Value != null)
                    {
                        if (parameter.Value == DBNull.Value)
                        {
                            command.Parameters.Add(parameter).Value = DBNull.Value;
                        }
                        else
                        {
                            switch (parameter.DbType)
                            {
                                case DbType.AnsiString:
                                case DbType.String:
                                {
                                    command.Parameters.Add(new SqlParameter(parameter.ParameterName, EscapeQuoteReg(parameter.Value.ToString())));
                                    continue;
                                }
                                case DbType.Binary:
                                {
                                    command.Parameters.Add(parameter.ParameterName, SqlDbType.Image).Value = parameter.Value;
                                    continue;
                                }
                            }
                            command.Parameters.Add(parameter);
                        }
                    }
                }
            }
            command.CommandText = strQuerry;
        }
    }
}

